function strToJson(str) {
    try {
        return JSON.parse(str)
    } catch (e) {
        console.log('字符串不是json 格式')
        return {}
    }
}

function jsonToStr(json) {
    if(typeof json === 'string') {
        return json
    }
    try {
        return JSON.stringify(json)
    } catch (e) {
        return json
    }
}

function jsonToStrFormat(json) {
    return JSON.stringify(json, null, 2)
}

function strFormat(str) {
    let obj = strToJson(str)
    if (jsonToStrFormat(obj) === '{}' && str !== '{}') {
        return str
    }
    return jsonToStrFormat(obj)
}

function initObj(obj) {
    let keys = Object.keys(obj)
    for (let i = 0; i < keys.length; i++) {
        let key = keys[i];
        let value = obj[key]
        if (typeof value === "object") {
            if (value === null || value === undefined) {
                continue
            }
            initObj(value)
        } else if (typeof value === "string") {
            obj[key] = ""
        }
    }
}

function dataToStr(date) {
    if (typeof date === 'string') {
        date = new Date(date)
    } else if (typeof date === 'number') {
        date = new Date(date)
    }
    try {
        let year = addZero(date.getFullYear());
        let month = addZero(date.getMonth() + 1);
        let day = addZero(date.getDate());
        let hour = addZero(date.getHours());
        let minute = addZero(date.getMinutes());
        let second = addZero(date.getSeconds());

        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ":" + second
    } catch (error) {
        console.log('error', date, error)
        return ''
    }
}

function addZero(num) {
    if (num < 10) {
        return '0' + num
    } else {
        return num
    }
}

function dataOnlyToStr(date) {
    if (typeof date === 'string') {
        date = new Date(date)
    }
    let year = addZero(date.getFullYear());
    let month = addZero(date.getMonth() + 1);
    let day = addZero(date.getDate());

    return year + '-' + month + '-' + day
}

function getStrLength(str) {
    let len = 0;
    for (let i = 0; i < str.length; i++) {
        let c = str.charCodeAt(i);
        //单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
            len++;
        } else {
            len += 2;
        }
    }
    return len;
}

function strIsJson(str) {
    try {
        // 尝试将字符串解析为 JSON 对象
        JSON.parse(str);
        // 如果没有抛出错误，则字符串可以被解析为一个对象
        return true;
    } catch (error) {
        // 如果抛出错误，则字符串不是一个有效的 JSON 对象
        return false;
    }
}

function strIsObjOrArray(str) {
    try {
        // 尝试将字符串解析为 JSON 对象
        let obj = JSON.parse(str);
        // 是对象或数组
        return (typeof obj === 'object' && obj !== null) || Array.isArray(obj);
    } catch (error) {
        // 如果抛出错误，则字符串不是一个有效的 JSON 对象
        return false;
    }
}

function cloneObj(obj) {
    if (obj === null) {
        return null
    }
    let newObj = {};
    if (obj instanceof Array) {
        newObj = [];
    }
    for (let key in obj) {
        let val = obj[key];
        newObj[key] = typeof val === 'object' ? cloneObj(val) : val;
    }
    return newObj;
}

// ---- str util start
function setValInfos(key, valInfos, obj) {
    if (valInfos.hasOwnProperty(key)) {
        // TODO 为了避免什么错误？ 影响数据切换了
        return
    }
    let info = {
        jsonStr: false,
        display: true
    }
    valInfos[key] = info

    if (typeof obj[key] === 'object') {
        info['type'] = 'object'
    } else if (typeof obj[key] === 'number') {
        info['type'] = 'number'
    } else if (typeof obj[key] === 'string') {
        info['type'] = 'string'
        info.jsonStr = strIsJson(obj[key])
    } else if (typeof obj[key] === 'boolean') {
        info['type'] = 'boolean'
    }
    info['className'] = info['type']
    if (info['jsonStr']) {
        info['className'] += '-true'
    }
}

function resetValInfos(valInfos, flag) {
    for (const valInfosKey in valInfos) {
        valInfos[valInfosKey]['display'] = flag
    }
}

const systemConst = {
    booleans: [true, false],
}

function setObjVal(obj, arr) {
    let keys = Object.keys(obj)
    let offset = 0
    let next = true
    out:
      for (let i = 0; i < arr.length && i < keys.length; i++) {
          if (arr[i] === 'pass' || keys[i + offset] === undefined) {
              continue
          }
          while (next) {
              let clazz = typeof obj[keys[i + offset]]
              if (clazz === 'string' || clazz === "undefined") {
                  obj[keys[i + offset]] = arr[i]
                  continue out;
              } else if (clazz === 'number') {
                  obj[keys[i + offset]] = Number(arr[i])
                  continue out;
              } else if (clazz === 'boolean') {
                  obj[keys[i + offset]] = Boolean(arr[i])
                  continue out;
              }
              offset++
              if (i + offset >= keys.length) {
                  break out;
              }
          }
          next = true
      }
}

function assignValues(fromObj, toObj, lvl) {
    // 防止无限递归
    if (lvl > 20) {
        return
    }

    for (let key in fromObj) {
        if (!toObj.hasOwnProperty(key)) {
            // 新增字段
            toObj[key] = fromObj[key]
            continue
        }

        if (typeof toObj[key] === 'object' && toObj[key] !== null) {
            if (typeof fromObj[key] === 'string' && fromObj[key].startsWith('{') && fromObj[key].endsWith('}')) {
                // 将字符串格式的JSON转换为对象
                toObj[key] = JSON.parse(fromObj[key]);
            } else {
                // 递归复制对象或数组
                toObj[key] = Array.isArray(fromObj[key]) ? [] : {};
                this.assignValues(fromObj[key], toObj[key], lvl + 1);
            }
        } else {
            // key
            toObj[key] = fromObj[key];
        }
    }
}

function copyTextIn(content) {
    if (window.clipboardData) {
        window.clipboardData.setData('text', content);
    } else {
        (function (content) {
            document.oncopy = function (e) {
                e.clipboardData.setData('text', content);
                e.preventDefault();
                document.oncopy = null;
            }
        })(content);
        document.execCommand('Copy');
    }
    return content;
}